#include "Polygon2.h"
#include "Tessellator.h"
#include "Platform.h"


Polygon2::Polygon2() {

}


void Polygon2::recalculate( Tessellator* tess ) {
	FOR ( const Contour2& c, mContours ) // VS2010 hard derp
		const_cast<Contour2&>(c).recalculateNormals();
	if ( tess )
		tess->tessellate( *this );
}


Point2 Polygon2::center() const {
	Vector2 sum( 0, 0 );
	FOR ( const Contour2& c, mContours )
		sum += c.center().fromOrigin();
	return Point2::origin + sum / mContours.size();
}


Polygon2::LockContours Polygon2::lockContours( Tessellator* t /*= 0 */ ) {
	return LockContours( *this, t );
}


const Polygon2::contour_array& Polygon2::contours() const {
	return mContours;
}


const Tessellation2& Polygon2::tessellation() const {
	return mInterior;
}


Polygon2::LockContours::LockContours( Polygon2& ref, Tessellator* t ) : target(ref), tess(t) {
}


Polygon2::LockContours::~LockContours() {
	if ( tess )
		tess->tessellate( target );
}

Polygon2::contour_array& Polygon2::LockContours::contours() {
	return target.mContours;
}
